package com.ejemplos;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

//import com.android.localiza.R;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class HerramientasActivity extends Activity implements LocationListener{
	  private TextView latituteField;
	  private TextView longitudeField;
	  private TextView logField;

	  private LocationManager locationManager;
	  private String provider;
	  private String Ruta;
	  private String strFecha;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.herramientas_edit);
	    latituteField = (TextView) findViewById(R.id.txtLatitudResult);
	    longitudeField = (TextView) findViewById(R.id.txtLongitudResult);
	    logField = (TextView) findViewById(R.id.txtLog);
	    
	    Ruta=this.getString(R.string.lblrutarespaldo);
	    // Get the location manager
	    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	    // Define the criteria how to select the locatioin provider -> use
	    // default
	    Criteria criteria = new Criteria();
	    provider = locationManager.getBestProvider(criteria, false);
	    Location location = locationManager.getLastKnownLocation(provider);

	    // Initialize the location fields
	    if (location != null) {
	      System.out.println("Provider " + provider + " has been selected.");
	      onLocationChanged(location);
	    } else {
	      latituteField.setText("Location not available");
	      longitudeField.setText("Location not available");
	    }
	    Date horaActual=new Date();

	    strFecha= String.valueOf(horaActual.getYear()+1900)+ "-" + String.valueOf(horaActual.getMonth()+1)+ "-" + String.valueOf(horaActual.getDate());

        
        
        
    }
	  /* Request updates at startup */
	  @Override
	  protected void onResume() {
	    super.onResume();
	    locationManager.requestLocationUpdates(provider, 3 * 60 * 1000 , 1, this);
	    //CADA 3 MINUTOS
	  }
	  /* Remove the locationlistener updates when Activity is paused */
	  @Override
	  protected void onPause() {
	    super.onPause();
	    locationManager.removeUpdates(this);
	  }
	  public void EscribeBitacora(String filename,String s)
	 {
			File file = null;
			OutputStream fop=null;
		 try {
				file = new File(filename);
				fop = new FileOutputStream(file,true);
				// if file doesnt exists, then create it
				if (!file.exists()) {
					file.createNewFile();
				}
	 
				// get the content in bytes
				byte[] contentInBytes = s.getBytes();
	           
				fop.write(contentInBytes);
				fop.flush();
				fop.close();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (fop != null) {
						fop.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		 
		 
		 
	 }
	  public void onLocationChanged(Location location) {
		  try{
			  double lat = (double) (location.getLatitude());
			  double lng = (double) (location.getLongitude());
			  latituteField.setText(String.valueOf(lat));
			  longitudeField.setText(String.valueOf(lng));
			  EscribeBitacora(Ruta + "loc2.txt",strFecha + "::(" +  String.valueOf(lat) +"::" +String.valueOf(lng) + ")" + "\n");
			  
			  //File myFile = new File(Ruta + "loc.txt");
				//myFile.createNewFile();
				//FileOutputStream fOut = new FileOutputStream(myFile);
				//OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
				//String strcad="";
				//	strcad="";
				//	strcad+=strFecha + "::(" +  String.valueOf(lat) +"::" +String.valueOf(lng) + ")";
				//	strcad+="\n";
				//	myOutWriter.append(strcad);
				//myOutWriter.close();
				//fOut.close();
			  
		  }  
		  catch (Exception e) {
			  logField.setText(e.getMessage());
		  }
	  }
	  public void onStatusChanged(String provider, int status, Bundle extras) {
	    // TODO Auto-generated method stub

	  }
	  public void onProviderEnabled(String provider) {
	    Toast.makeText(this, "Enabled new provider " + provider,
	        Toast.LENGTH_SHORT).show();

	  }
	  public void onProviderDisabled(String provider) {
	    Toast.makeText(this, "Disabled provider " + provider,
	        Toast.LENGTH_SHORT).show();
	  }



}